using System;
using System.Collections.Generic;
using System.Text;
using GameStateManager;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace ScreenTest.Screens
{
    class LoadingScreen : GameScreen
    {
        EventHandler<EventArgs> loadNextScreen;     //The next screen to load
        bool otherScreensAreGone = false;
        ContentManager content = null;
        SpriteFont loadingFont;

        public static void Load(ScreenManager screenManager, EventHandler<EventArgs> loadNextScreen)
        {
            foreach (GameScreen screen in screenManager.GetScreens())
            {
                screen.ExitScreen();
            }

            LoadingScreen loadingScreen = new LoadingScreen();
            loadingScreen.loadNextScreen = loadNextScreen;

            screenManager.AddScreen(loadingScreen);
        }

        public override void LoadGraphicsContent(bool loadAllContent)
        {
            if (content == null)
            {
                content = new ContentManager(ScreenManager.Game.Services);
                loadingFont = content.Load<SpriteFont>(@"Fonts\LoadingFont");
            }
        }

        public override void UnLoadGraphicsContent(bool unloadAllContent)
        {
            content.Unload();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus)
        {
            if (otherScreensAreGone)
            {
                ScreenManager.RemoveScreen(this);
                loadNextScreen(this, EventArgs.Empty);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.GraphicsDevice.Clear(Color.Black);

            if (ScreenManager.GetScreens().Length == 1)
            {
                otherScreensAreGone = true;
            }

            const string message = "Loading ...";

            ScreenManager.SpriteBatch.Begin();
            ScreenManager.SpriteBatch.DrawString(loadingFont, message, new Vector2(100, 100), Color.White);
            ScreenManager.SpriteBatch.End();
        }
    }
}
